namespace NHCodeFirst.FluentMapper
{
    using System;
    using Enums;
    using HbmModel;

    public class TimeStampMapper
    {
        private readonly TimeStamp timeStamp;

        public TimeStampMapper(TimeStamp timeStamp)
        {
            this.timeStamp = timeStamp;
        }

        public TimeStampMapper ToColumn(string value)
        {
            timeStamp.Column = value;
            return this;
        }

        public TimeStampMapper Generated(VersionGenerated generated)
        {
            timeStamp.Generated = generated.ToString().ToLower();
            return this;
        }

        public TimeStampMapper Access(Access access, Naming? namingStrategy = null)
        {
            return Access(access.ToString().ToLower(), namingStrategy);
        }

        public TimeStampMapper Access<T>(Naming? namingStrategy = null)
        {
            return Access(typeof (T).FullName, namingStrategy);
        }

        public TimeStampMapper Access(Type type, Naming? namingStrategy = null)
        {
            return Access(type.FullName, namingStrategy);
        }

        private TimeStampMapper Access(string access, Naming? naming)
        {
            timeStamp.Access = access;
            if (naming.HasValue)
                timeStamp.Access += "." + EnumsToHbmString.GetAccessNamingStrategy(naming.Value);
            return this;
        }

        public TimeStampMapper Source(TimeStampSource value)
        {
            timeStamp.Source = value.ToString().ToLower();
            return this;
        }

        public TimeStampMapper UnsavedValue(object value)
        {
            timeStamp.UnsavedValue = value == null ? "null" : value.ToString();
            return this;
        }
    }
}